//
//  HomeViewController.m
//  HaiKangDemo
//
//  Created by 崔 on 16/10/3.
//  Copyright © 2016年 崔. All rights reserved.
//

#import "HomeViewController.h"
#import <UIImageView+WebCache.h>
#import "LGControllerViewController.h"
#import <MBProgressHUD.h>
#import <SVProgressHUD.h>
#import "ALSHeader.h"
#define CHYPICNO 4

@interface HomeViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scollerView;
@property (weak, nonatomic) IBOutlet UIPageControl *PageControl;
//@property (weak, nonatomic) IBOutlet UIView *doorControllerView;
//@property (weak, nonatomic) IBOutlet UIImageView *tempImage;
@property (nonatomic ,strong)NSTimer *timer;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加图片轮播器
    [self addWheel];
    //    添加定时器
    [self addTimer];
    //当有NAV控制器时,不自动调整内容中 scollerView 的向下偏移
    self.automaticallyAdjustsScrollViewInsets = NO;

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(als3DTouch) name:ALS3DTouchCameraNotification object:nil];
    
}

- (void)als3DTouch {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:@"homeVCToCameraVC" sender:nil];
    });
}


//进出记录
- (IBAction)recordClick {
    UIStoryboard *Record = [UIStoryboard storyboardWithName:@"Record" bundle:nil];
    UIViewController *RecordVC = [Record instantiateInitialViewController];
    [self.navigationController pushViewController:RecordVC animated:YES];
    
    
//    [SVProgressHUD showInfoWithStatus:@"正在开发中..."];
    
//    [self closeHint];
}

//查询更多
- (IBAction)moreClick {
    
    [SVProgressHUD showInfoWithStatus:@"正在开发中..."];
    
    [self closeHint];
}

//关闭提示
- (void)closeHint{
    
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
        });
    });
    
    
}
///点击门禁控制器
- (IBAction)doorContrllerClick:(UIButton *)sender {
    
//    NSLog(@"doorcontroller");
    
    
//    return;
    
//    [self changeTohighlight:self.doorControllerView enable:NO];
    
    //跳转到选择添加控制器的方式(手动/搜索)
//    LGControllerViewController *changeAddVC = [[LGControllerViewController alloc] init] ;
    
//    [self.navigationController pushViewController:changeAddVC animated:YES];
    
}

- (void)addWheel{
    
    self.scollerView.delegate = self;
    
    CGFloat imageW = [UIScreen mainScreen].bounds.size.width;
    
    CGFloat imageH = imageW / 1.7;
    
    CGFloat imageY = 0;
    
    for (int i = 0; i < CHYPICNO ; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_0%d.jpg",i]];
        
        imageView.tag = i;
        
        //手势监听器
        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewCick:)];
        
        [imageView addGestureRecognizer:ges];
        
        imageView.userInteractionEnabled = YES;
        
        imageView.contentMode = UIViewContentModeScaleToFill;
        
        CGFloat imageX = i * imageW;
        
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        
        [self.scollerView addSubview:imageView];
        
    }
    
    self.scollerView.contentSize = CGSizeMake(CHYPICNO*imageW, 0);
    
    self.scollerView.pagingEnabled = YES;
    
    self.scollerView.showsHorizontalScrollIndicator = NO;
    
    self.scollerView.showsVerticalScrollIndicator = NO;
    
    self.PageControl.numberOfPages = CHYPICNO;
}


#pragma mask-代理方法
// 正在滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scrollW = self.scollerView.frame.size.width;
    
    int page = (self.scollerView.contentOffset.x + scrollW*0.5) / scrollW;
    
    self.PageControl.currentPage = page;
    
}
//开始拖动时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self removeTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self addTimer];
}
//添加定时器
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    //多任务处理
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
//移除定时器
- (void)removeTimer
{
    [self.timer invalidate];
    
    self.timer = nil;
}
//下一张
- (void)nextImage
{
    NSInteger page = 0;
    if (self.PageControl.currentPage == CHYPICNO - 1) {
        
        page = 0;
        
    }else{
        
        page = self.PageControl.currentPage + 1;
        
    }
    CGFloat offsetX = page * self.scollerView.frame.size.width;
    
    CGPoint offset = CGPointMake(offsetX, 0);
    
    [self.scollerView setContentOffset:offset animated:YES];
    
}
//这里处理处理轮播器,点击跳转到WebView
- (void)imageViewCick:(UITapGestureRecognizer * )sender{
    
    //[sender view] 被点击的View
//    CHYLog(@"%d",[sender view].tag);
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [self removeTimer];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

}
@end
